ccR_GUI = LibStub("AceAddon-3.0"):NewAddon("ccR_GUI", "AceConsole-3.0", "AceEvent-3.0")

local gui_title = {
	name = "ccR",
	handler = ccR_GUI,
	type = "group",
	args = {
        emptyspace = {
            order = 0,
            name = "",
            type = "description",
        },
	},

}

local gui_settings = {
	name = "ccR Settings",
	order = 0,
	handler = ccR_GUI,
	type = "group",
	args = {
    	EnableccR = {
    		order = 20,
    		type = "toggle",
    		name = "Enable",
    		desc = "Run rotations on keypress",
    		set = function(info, value) ccRS.Enabled = value end,
    		get = function(info) return ccRS.Enabled end,
    	},
        emptyspace21 = {
            order = 21,
            name = "",
            type = "description",
        },
        ToggleFrameCount = {
            order = 31,
            type = "toggle",
            name = "Run per Frame Count",
            desc = "Run the rotation every X frames.",
            set = function(info, value) ccRS.RunPerFrameCount = value end,
            get = function(info) return ccRS.RunPerFrameCount end,
        },
        FrameCountInput = {
            order = 32,
            type = "input",
            name = "Frames",
            desc = "How many frames to wait until execution?\n\nDefault: 25.",
            set = function(info, value) ccRS.FrameCountMax = tonumber(value) end,
            get = function(info) return tostring(ccRS.FrameCountMax) end,
        },
        emptyspace33 = {
            order = 33,
            name = "",
            type = "description",
        },
        ToggleTimeWait = {
            order = 34,
            type = "toggle",
            name = "Run on time waited.",
            desc = "Run the rotation after waiting X.",
            set = function(info, value) ccRS.RunPerTimeCount = value end,
            get = function(info) return ccRS.RunPerTimeCount end,
        },
        TimeCountInput = {
            order = 35,
            type = "input",
            name = "Time",
            desc = "How much time to wait until execution?\n\nDefault: 0.10.",
            set = function(info, value) ccRS.TimeCountMax = tonumber(value) end,
            get = function(info) return tostring(ccRS.TimeCountMax) end,
        },
        emptyspace39 = {
            order = 39,
            name = "",
            type = "description",
        },        
        Header2 = {
            order = 40,
            type = "header",
            name = "Automatic Features",
        },
        ToggleAutoAoE = {
            order = 41,
            type = "toggle",
            name = "Use Auto AoE",
            desc = "If specifically written in the rotation, automatically switch to AoE mode.",
            set = function(info, value) ccRS.Auto_AoE = value end,
            get = function(info) return ccRS.Auto_AoE end,
        },
        ToggleIgnoreAoEOnBoss = {
            order = 42,
            type = "toggle",
            name = "Ignore AoE on boss",
            desc = "If using Auto AoE, This will keep to single target rotation if selecting a boss.",
            set = function(info, value) ccRS.Auto_AoE_Ignore_On_Boss = value end,
            get = function(info) return ccRS.Auto_AoE_Ignore_On_Boss end,
        },
        emptyspace49 = {
            order = 49,
            name = "",
            type = "description",
        },        
    	ToggleAutoLoot = {
    		order = 50,
    		type = "toggle",
    		name = "Use Auto Loot",
    		desc = "Automatically loot a corpse when in range",
    		set = function(info, value) ccRS.Auto_Loot = value end,
    		get = function(info) return ccRS.Auto_Loot end,
    	},
        emptyspace51 = {
            order = 51,
            name = "",
            type = "description",
        },      
    	ToggleAutoTarget = {
    		order = 60,
    		type = "toggle",
    		name = "Use Auto Target",
    		desc = "Automatically target a hostile npc infront of you.",
    		set = function(info, value) ccRS.Auto_Target = value end,
    		get = function(info) return ccRS.Auto_Target end,
    	},
        emptyspace61 = {
            order = 61,
            name = "",
            type = "description",
        },
    	ToggleAutoFace = {
    		order = 70,
    		type = "toggle",
    		name = "Use Auto Facing",
    		desc = "Automatically face your target.",
    		set = function(info, value) ccRS.Auto_Facing = value end,
    		get = function(info) return ccRS.Auto_Facing end,
    	},
        emptyspace71 = {
            order = 71,
            name = "",
            type = "description",
        },
        ToggleStartAttack = {
            order = 72,
            type = "toggle",
            name = "Start Attack on Target",
            desc = "When running the rotation, Enable StartAttack as soon as you have a target.",
            set = function(info, value) ccRS.StartAttack_On_Target = value end,
            get = function(info) return ccRS.StartAttack_On_Target end,
        },
        emptyspace73 = {
            order = 73,
            name = "",
            type = "description",
        },
        ToggleTrinkets = {
            order = 74,
            type = "toggle",
            name = "Use Trinkets",
            desc = "Use Trinkets in combat.",
            set = function(info, value) ccRS.Trinkets_In_Combat = value end,
            get = function(info) return ccRS.Trinkets_In_Combat end,
        },        
        Header3 = {
            order = 79,
            type = "header",
            name = "Racial Features",
        },
    	ToggleRacialInteruption = {
    		order = 80,
    		type = "toggle",
    		name = "Use Racial spell interuption in combat.",
    		desc = "Automatically use racial spell interuption.\n - Arcane Torrent\n - Warstomp\n - Quaking Palm",
    		set = function(info, value) ccRS.Racial_Spell_Interupt = value end,
    		get = function(info) return ccRS.Racial_Spell_Interupt end,
    	},
        emptyspace81 = {
            order = 81,
            name = "",
            type = "description",
        },
    	ToggleRacialHealing = {
    		order = 90,
    		type = "toggle",
    		name = "Use Racial Healing in combat.",
    		desc = "Automatically use racial healing.\n - Gift of the Naaru",
    		set = function(info, value) ccRS.Racial_Healing = value end,
    		get = function(info) return ccRS.Racial_Healing end,
    	},
        emptyspace91 = {
            order = 91,
            name = "",
            type = "description",
        },
    	ToggleRacialDPS = {
    		order = 100,
    		type = "toggle",
    		name = "Use Racial DPS",
    		desc = "Automatically use racial dps abilities in combat.\n - Blood Fury\n - Berserking",
    		set = function(info, value) ccRS.Racial_DPS = value end,
    		get = function(info) return ccRS.Racial_DPS end,
    	},
        emptyspace101 = {
            order = 101,
            name = "",
            type = "description",
        },
    	ToggleRacialDebuffRemoval = {
    		order = 110,
    		type = "toggle",
    		name = "Use Racial Debuff Removal",
    		desc = "Automatically use racial debuff removal in combat.\n - Stoneform \n - Escape Artist.",
    		set = function(info, value) ccRS.Racial_Debuff = value end,
    		get = function(info) return ccRS.Racial_Debuff end,
    	},
    },
}

local gui_customfeatures = {
	name = "Custom",
	order = 1,
	handler = ccR_GUI,
	type = "group",
	args = {
        ToggleKafaPress = {
            order = 10,
            type = "toggle",
            name = "Use Kafa Press",
            desc = "Use the Kafa Press if available in combat.",
            set = function(info, value) ccRS.CF_KafaPress = value end,
            get = function(info) return ccRS.CF_KafaPress end,
        },
        blank11 = {
            order = 11,
            name = "",
            type = "description",
        },        
        ToggleKafakotaBerry = {
            order = 20,
            type = "toggle",
            name = "Use Kafakota Berries",
            desc = "Use the Kafakota Berries when in combat.",
            set = function(info, value) ccRS.CF_Kafakota_Berry = value end,
            get = function(info) return ccRS.CF_Kafakota_Berry end,
        },
        blank21 = {
            order = 21,
            name = "",
            type = "description",
        },        
        ToggleMOPTracking = {
            order = 30,
            type = "toggle",
            name = "Track MOP Treasures",
            desc = "Automatically track on minimap for MoP BoA items and Treasures.",
            set = function(info, value) ccRS.CF_Treasure_Tracking = value end,
            get = function(info) return ccRS.CF_Treasure_Tracking end,
        },
        blank31 = {
            order = 31,
            name = "",
            type = "description",
        },        
        ToggleTargetTracking = {
            order = 40,
            type = "toggle",
            name = "Track Target",
            desc = "Automatically track on minimap your target.",
            set = function(info, value) ccRS.CF_Target_Tracking = value end,
            get = function(info) return ccRS.CF_Target_Tracking end,
        },
        blank41 = {
            order = 41,
            name = "",
            type = "description",
        },        
        ToggleHealthPotion = {
            order = 50,
            type = "toggle",
            name = "Use Health Potions",
            desc = "Automatically use a health potion.",
            set = function(info, value) ccRS.CF_Health_Potions = value end,
            get = function(info) return ccRS.CF_Health_Potions end,
        },
        InputHealthPercentage = {
            order = 55,
            type = "input",
            name = "Health Percentage",
            desc = "Automatically use a health potion on percentage of health.",
            set = function(info, value) ccRS.CF_Health_PotionsHP = tonumber(value) end,
            get = function(info) return tostring(ccRS.CF_Health_PotionsHP) end,
        },
        blank59 = {
            order = 59,
            name = "",
            type = "description",
        },         
        ToggleManaPotion = {
            order = 60,
            type = "toggle",
            name = "Use Mana Potions",
            desc = "Automatically use a Mana potion.",
            set = function(info, value) ccRS.CF_Mana_Potions = value end,
            get = function(info) return ccRS.CF_Mana_Potions end,
        },
        InputManaPercentage = {
            order = 65,
            type = "input",
            name = "Mana Percentage",
            desc = "Automatically use a mana potion on percentage of mana.",
            set = function(info, value) ccRS.CF_Mana_PotionsMP = tonumber(value) end,
            get = function(info) return tostring(ccRS.CF_Mana_PotionsMP) end,
        },
        blank69 = {
            order = 69,
            name = "",
            type = "description",
        },
        ToggleStayPotted = {
            order = 70,
            type = "toggle",
            name = "Use Alchemy Flask",
            desc = "Automatically use and keep the Alchemy Flask buff.",
            set = function(info, value) ccRS.CF_Stay_Potted = value end,
            get = function(info) return ccRS.CF_Stay_Potted end,
        },
        blank79 = {
            order = 79,
            name = "",
            type = "description",
        },
        ToggleEngineeringBombs = {
            order = 80,
            type = "toggle",
            name = "Use Engineering Bombs",
            desc = "Automatically use Engineering Bombs in combat.",
            set = function(info, value) ccRS.CF_Engineering_Bombs = value end,
            get = function(info) return ccRS.CF_Engineering_Bombs end,
        },
        blank89 = {
            order = 89,
            name = "",
            type = "description",
        },
        ToggleTrackGoodLoot = {
            order = 90,
            type = "toggle",
            name = "Track high value gathering items.",
            desc = "Track the following:\n -Chests \n - Rares \n - Golden Lotus (Herbalism)\n - Trillium (Mining) ",
            set = function(info, value) ccRS.CF_Track_Good_Lewt = value end,
            get = function(info) return ccRS.CF_Track_Good_Lewt end,
        },        
        blank91 = {
            order = 91,
            name = "",
            type = "description",
        },
        ToggleAutoJoinLFG = {
            order = 100,
            type = "toggle",
            name = "Auto Join LFG.",
            desc = "Never miss a dungeon or raid invite again! ",
            set = function(info, value) ccRS.CF_AutoJoinLFG = value end,
            get = function(info) return ccRS.CF_AutoJoinLFG end,
        }, 
	},
}

local gui_endgame = {
    name = "Endgame",
    order = 1,
    handler = ccR_GUI,
    type = "group",
    args = {
        guiUseAnkhyOrG1zStar = {
            order = 1,
            type = "select",
            name = "End Game Rotations",
            desc = "Which End Game Rotations to use?",
            style = "dropdown",
            values = {"ccR", "Ankhy", "G1zStar"},
            set = function(info, value) ccRS.UseAnkhyorG1zStar = value end,
            get = function(info) return ccRS.UseAnkhyorG1zStar end,
        },
        -- guiUseAnkhy = {
        --     order = 10,
        --     type = "toggle",
        --     name = "Use Ankhy's",
        --     desc = "Use Ankhy's Rotations if available.",
        --     set = function(info, value) ccRS.UseAnkhy = value end,
        --     get = function(info) return ccRS.UseAnkhy end,
        -- },
        -- gui_BlankSpace11 = {
        --     order = 11,
        --     type = "description",
        --     name = "",
        -- },        
        -- guiUseG1zstar = {
        --     order = 20,
        --     type = "toggle",
        --     name = "Use G1zstar's",
        --     desc = "Use G1zstar's Rotations if available.",
        --     set = function(info, value) ccRS.UseG1zStar = value end,
        --     get = function(info) return ccRS.UseG1zStar end,
        -- },
        guiEndgameSelectAnOption = {
            order = 49,
            type = "header",
            name = "Choose an Option",
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 and ccRS.UseAnkhyorG1zStar ~= 2 then return false else return true end end
            },
        gui_AnkhyHeader = {
            order = 50,
            type = "header",
            name = "Ankhy's Settings",
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 and ccRS.UseAnkhyorG1zStar ~= 2 then return true end if ccRS.UseAnkhyorG1zStar == 3 then return true elseif ccRS.UseAnkhyorG1zStar == 2 then return false end end
        },
		tblIgniteMunch = {
			order = 51,
			type = "input",
			name = "Ignite Spellpower",
			desc = "What spellpower to use combustion on a ignited target?",
			set = function(info, value) ccRS.Ankhy_Mage_Ignite = value end,
			get = function(info) return ccRS.Ankhy_Mage_Ignite end,
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 and ccRS.UseAnkhyorG1zStar ~= 2 then return true end if ccRS.UseAnkhyorG1zStar == 3 then return true elseif ccRS.UseAnkhyorG1zStar == 2 then return false end end,
		},
        gui_G1zstarHeader = {
            order = 100,
            type = "header",
            name = "G1zstar's Settings",
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
        },
        G1zStarTauntTrainer = {
            order = 101,
            type = "toggle",
            name = "Taunt",
            desc = "Use SoO Automatic Taunt?",
            descStyle = "inline",
            get = function(info) return ccRS.G1zStarTaunt end,
            set = function(info, value) ccRS.G1zStarTaunt = value end,
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
        },
        G1zStarInterruptTrainer = {
            order = 102,
            type = "toggle",
            name = "Interrupt",
            desc = "Use SoO Automatic Interrupt?",
            descStyle = "inline",
            get = function(info) return ccRS.G1zStarInterrupt end,
            set = function(info, value) ccRS.G1zStarInterrupt = value end,
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
        },
        G1zStarThokThrottle = {
            order = 103,
            type = "toggle",
            name = "Thok",
            desc = "Use automatic stop casting for Thok? (Not tested)",
            descStyle = "inline",
            get = function(info) return ccRS.G1zStarThokThrottle end,
            set = function(info, value) ccRS.G1zStarThokThrottle = value end,
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
        },
        G1zStarTimelessIsle = {
            order = 104,
            type = "execute",
            name = "Timeless Isle Buff Uptime",
            desc = "Uses the timeless isle buffs and crystal of insanity while on the isle.",
            descStyle = "inline",
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 then return true end end,
            func = function() if not ccRS.G1zStarTimelessIsle then print("TimelessIsleBuffs script loaded.") SetTimerCallback(G1zStarTimelessIsleBuffsUpTime,  50) ccRS.G1zStarTimelessIsle = true return else print("TimelessIsleBuffs script unloaded.") RemoveTimerCallback(G1zStarTimelessIsleBuffsUpTime) ccRS.G1zStarTimelessIsle = false return end end
        },
        G1zStarSlowFall = {
            order = 105,
            type = "execute",
            name = "Slow Fall",
            desc = "Prevents fall damage (disable if doing siegecrafter belts.)",
            descStyle = "inline",
            hidden = function() if ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
            func = function() if not ccRS.G1zStarSlowfall then print("Slowfall script loaded.") SetTimerCallback(G1zStarSlowfallfunc, 50) ccRS.G1zStarSlowfall = true return else print("Slowfall script unloaded.") RemoveTimerCallback(G1zStarSlowfallfunc) ccRS.G1zStarSlowfall = false return end end
        },
        G1zStarProtectionHeader = {
            order = 120,
            type = "header",
            name = "Protection Paladin Rotation Settings",
            hidden = function() if ccRS.ClassPlayer ~= 2 then return true elseif ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
        },
        G1zStarUseDivineProtection = {
            order = 121,
            type = "toggle",
            name = "DP",
            desc = "Use Divine Protection on Cooldown?",
            descStyle = "inline",
            get = function(info) return ccRS.G1zStarProtectionPaladinDP end,
            set = function(info, value) ccRS.G1zStarProtectionPaladinDP = value end,
            hidden = function() if ccRS.ClassPlayer ~= 2 then return true elseif ccRS.UseAnkhyorG1zStar ~= 3 then return true elseif ccRS.UseAnkhyorG1zStar == 3 then return false end end,
        }
    },    
}

local options = {
    name = "MyAddon",
    type = "group",
    get = function() return random(2) == 2 end,
    set = function(info, value) print(info.name, value and "ENABLED" or "DISABLED") end,
    args = {
        usual = {
            name = "Warrior",
            type = "group",
            childGroups = "tabs",
            args = {
                Arms = {
                    name = "Arms",
                    type = "group",
                    childGroups = "tree",
                    args = {
                        tabby = {
                            name = "Tabby",
                            type = "toggle",
                        },
                        torty = {
                            name = "Tortoiseshell",
                            type = "toggle",
                        },
                    },
                },
                Fury = {
                    name = "Fury",
                    type = "group",
                    childGroups = "tree",
                    args = {
                        terrier = {
                            name = "Terrier",
                            type = "toggle",
                        },
                        pug = {
                            name = "Pug",
                            type = "toggle",
                        },
                    },
                },
                Protection = {
                    name = "Protection",
                    type = "group",
                    childGroups = "tree",
                    args = {
                        terrier = {
                            name = "Terrier",
                            type = "toggle",
                        },
                        pug = {
                            name = "Pug",
                            type = "toggle",
                        },
                    },
                },
            },
        },
        rare = {
            name = "Rare",
            type = "group",
            args = {
                squirfly = {
                    name = "Flying Squirrel",
                    type = "toggle",
                }
            }
        }
    }
}

function ccR_GUI:OnInitialize()

    --[[
    LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("MyAddon", options)
    -- Top level "MyAddon" in the Interface Options list:
    LibStub("LibAboutPanel").new(nil, "MyAddon")
    -- Second level "Usual" in the Interface Options list:
    LibStub("AceConfigDialog-3.0"):AddToBlizOptions("MyAddon", options.args.usual.name, "MyAddon", "usual")
    -- Second level "Rare" in the Interface Options list:
    LibStub("AceConfigDialog-3.0"):AddToBlizOptions("MyAddon", options.args.rare.name, "MyAddon", "rare")
    ]]

	-- Load Settings Table
	self.db = LibStub("AceDB-3.0"):New("ccRS")

	-- Create options
	local AC = LibStub("AceConfig-3.0")
    local ACD = LibStub("AceConfigDialog-3.0")
	AC:RegisterOptionsTable("ccR_Title", gui_title)
    -- Add to Interface
    ACD:AddToBlizOptions("ccR_Title", "ccR")

    -- Show only the class we are playing
    if ccrPlayerClass() == "Death Knight" then
        AC:RegisterOptionsTable("ccR_DeathKnight", gui_deathknight)
        ACD:AddToBlizOptions("ccR_DeathKnight", "Death Knight", "ccR")
    end
    if ccrPlayerClass() == "Druid" then
        AC:RegisterOptionsTable("ccR_Druid", gui_druid)
        ACD:AddToBlizOptions("ccR_Druid", "Druid", "ccR")
    end
    if ccrPlayerClass() == "Hunter" then
        AC:RegisterOptionsTable("ccR_Hunter", gui_hunter)
        ACD:AddToBlizOptions("ccR_Hunter", "Hunter", "ccR")
    end
    if ccrPlayerClass() == "Mage" then
        AC:RegisterOptionsTable("ccR_Mage", gui_mage)
        ACD:AddToBlizOptions("ccR_Mage", "Mage", "ccR")
    end
    if ccrPlayerClass() == "Monk" then
        AC:RegisterOptionsTable("ccR_Monk", gui_monk)
        ACD:AddToBlizOptions("ccR_Monk", "Monk", "ccR")
    end
    if ccrPlayerClass() == "Paladin" then
        AC:RegisterOptionsTable("ccR_Paladin", gui_paladin)
        ACD:AddToBlizOptions("ccR_Paladin", "Paladin", "ccR")
    end
    if ccrPlayerClass() == "Priest" then
        AC:RegisterOptionsTable("ccR_Priest", gui_priest)
        ACD:AddToBlizOptions("ccR_Priest", "Priest", "ccR")
    end
    if ccrPlayerClass() == "Rogue" then
        AC:RegisterOptionsTable("ccR_Rogue", gui_rogue)
        ACD:AddToBlizOptions("ccR_Rogue", "Rogue", "ccR")
    end
    if ccrPlayerClass() == "Shaman" then
        AC:RegisterOptionsTable("ccR_Shaman", gui_shaman)
        ACD:AddToBlizOptions("ccR_Shaman", "Shaman", "ccR")
    end
    if ccrPlayerClass() == "Warlock" then
        AC:RegisterOptionsTable("ccR_Warlock", gui_warlock)
        ACD:AddToBlizOptions("ccR_Warlock", "Warlock", "ccR")
    end
    if ccrPlayerClass() == "Warrior" then
        ACD:AddToBlizOptions("ccR_Warrior", "Warrior", "ccR")
        AC:RegisterOptionsTable("ccR_Warrior", gui_warrior)
    end
    AC:RegisterOptionsTable("ccR_Endgame", gui_endgame)
    AC:RegisterOptionsTable("ccR_Settings", gui_settings)
    AC:RegisterOptionsTable("ccR_CF", gui_customfeatures)
    ACD:AddToBlizOptions("ccR_Endgame", "Endgame", "ccR")
    ACD:AddToBlizOptions("ccR_CF", "Custom Features", "ccR")
    ACD:AddToBlizOptions("ccR_Settings", "Settings", "ccR")
end